<template>
  <div>
    <el-card shadow="hover" body-style="width:100%;" style="margin-bottom: 20px">
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0"
      @click="showDialog"
    >新增分类</el-button>
    </el-card>
    <el-card shadow="hover" body-style="width:100%;">
    <el-table :data="cateList" style="width: 100%" border stripe :header-cell-class-name="'headerBg'">
      <el-table-column label="序号" width="60" align="center" type="index" />
      <el-table-column prop="name" label="分类名称" width="width" />

      <el-table-column prop="prop" label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateCate(scope.row)"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteCate(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="cateForm.id ? '修改分类' : '添加分类'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="ruleForm"
        :model="cateForm"
        style="width: 80%"
        :rules="rules"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="cateForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateCate">确 定</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'CateList',
  data() {
    return {
      dialogFormVisible: false,
      cateList: [],
      cateForm: {
        name: ''
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, message: '分类至少一个字', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const res = await this.$API.catelist.reqCateList()
      if (res.code === 200) {
        this.cateList = res.data
      }
    },

    showDialog() {
      // 显示对话框
      this.dialogFormVisible = true
      // 如果点了取消会清空数据，每次打开都会为空
      this.cateForm = {
        name: ''
      }
    },
    updateCate(row) {
      this.dialogFormVisible = true
      this.cateForm = { ...row }
    },
    async addOrUpdateCate() {
      this.dialogFormVisible = false
      const res = await this.$API.catelist.reqAddOrUpdateCate(this.cateForm)
      console.log('@@@@@addorupdate--res', res)

      if (res.code === 200) {
        // 有id 修改成功 没有id 添加成功
        this.$message.success(
          this.cateForm.id ? '修改分类成功' : '添加分类成功'
        )

        this.getCateList()
      }
    },
    deleteCate(row) {
      this.$confirm('确定删除该分类？删除后该分类下的菜品也将被删除', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$API.catelist.reqRemoveCate(row.id)
          if (res.code === 200) {
            this.$message.success('删除成功!')
            this.getCateList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'warning',
            message: '你取消了删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.headerBg {
  background: #eee!important;
}
</style>
